//
//  HJPayViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJPayViewController.h"
#import "HJAskToBuyViewController.h"

#import "HJSellViewController.h"

@interface HJPayViewController ()
@property (weak, nonatomic) IBOutlet UILabel *payMoney;
@property (weak, nonatomic) IBOutlet UILabel *bankCard;

@end

@implementation HJPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.payMoney.text = [NSString stringWithFormat:@"￥%@",self.pay];
    [mNotificationCenter addObserver:self selector:@selector(didWeChatPaySuccessNoti) name:@"didWeChatPaySuccessNoti" object:nil];
}


- (IBAction)bankPay:(UIButton *)sender {
    
    [[HJPayManager manager]bankPayWithStartPay:@"" model:@"" viewController:self];
    
}

- (IBAction)weChatPay:(UIButton *)sender {
    if (![WXApi isWXAppInstalled]) {//判断是否有微信
        [self showErrorHUDWithError:@"尚未安装微信"];
        return;
    }
    [self showTextHUDWithPleaseWait];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"order_sn"] = self.order_sn;
    params[@"type"] = @"1";
    params[@"order_type"] = self.order_type;
    [TZHttpTool post:PayAPI params:params success:^(NSDictionary *result) {
        [self hideTextHud];
        [[HJPayManager manager] wechatPayWithDic:result[@"parameters"]];
    } failure:^(NSString *msg) {
        [self hideTextHud];
        [self showErrorHUDWithError:msg];
    }];
}

- (IBAction)aliPay:(UIButton *)sender {
    [HJPayManager manager].out_trade_no = self.order_sn;
    [HJPayManager manager].order_type = self.order_type;
    [[HJPayManager manager] zhifubaoWithPaysuccess:^(NSDictionary *dic) {
        [self didClick];
    } failure:^(NSString *msg) {
        [self hideTextHud];
        [self showErrorHUDWithError:msg];
    }];
}
-(void)didWeChatPaySuccessNoti {
    [self didClick];
}
-(void)didClick {
    if ([self.order_type isEqualToString:@"3"] || [self.order_type isEqualToString:@"4"]) {
        HJAskToBuyViewController * asktobuyVC = [[HJAskToBuyViewController alloc]init];
        asktobuyVC.navTitle = @"我的求购和购买";
        if ([self.order_type isEqualToString:@"3"]) {
            asktobuyVC.num_tag = @"10";
        }else {
            asktobuyVC.num_tag = @"11";
        }
        [self.navigationController pushViewController:asktobuyVC animated:YES];
    }else {
        HJSellViewController *sellVC = [[HJSellViewController  alloc]init];
        sellVC.navTitle = @"我的出售";
        [self.navigationController pushViewController:sellVC animated:YES];
    }
}

-(void)dealloc {
    [mNotificationCenter removeObserver:self];
}
@end
